// @ts-ignore
import request from '@/utils/request'

/**
 * 获取钉钉JSAPI授权信息
 * @param url 当前页面URL
 * @returns 授权信息
 */
export const jsSdkAuthorized = (url: string) => {
  return request({
    url: '/jsSdkAuthorized',
    method: 'get',
    params: {
      url,
    },
  })
}

/**
 * 测试API连接
 * @returns 测试结果
 */
export const testApiConnection = () => {
  return request({
    url: '/dingding',
    method: 'get',
  })
}

export const getAccessToken = () => {
  return request({
    url: '/getAccessToken',
    method: 'get',
  })
}

/**
 * 获取钉钉用户信息
 * @param code 授权码
 * @returns 用户信息
 */
export const fetchDingUserInfo = (code: string) => {
  // 模拟API返回
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        data: {
          dingUserInfo: {
            id: '1',
            name: '张三',
            position: '前台接待',
            // 其他钉钉用户信息字段
          },
          auth_token: 'mock-token-123456'
        }
      })
    }, 500)
  })
}

/**
 * 用户登录
 * @param code 授权码
 * @returns 登录结果
 */
export const login = (code: string) => {
  return request({
    url: '/dingtalk/login',
    method: 'post',
    params: {
      code
    }
  })
}

/**
 * 用户登录
 * @param code 授权码
 * @returns 登录结果
 */
export const loginAuth = () => {
  return request({
    url: '/api/auth/index',
    method: 'get'
  })
}
